package doantotnghiep.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.ProviderBusiness;
import doantotnghiep.model.Provider;
import doantotnghiep.modelDTO.ProviderDTO;

@Controller
public class ProviderManagController {

	@Autowired
	private ProviderBusiness providerBusiness;
	
	private ProviderDTO providerDTO;
	
	private Provider provider;

	@ModelAttribute("ProviderDTO")
	public ProviderDTO getProviderObject() {
		return new ProviderDTO();
	}

	@RequestMapping(value = "/viewProvider", method = RequestMethod.GET)
	public ModelAndView viewAllProvider() {
		List<Provider> providers;
		if(providerBusiness != null){
			providers = providerBusiness.listProvider();
			if(providers != null){
				List<ProviderDTO> providerDTOs = new ArrayList<ProviderDTO>();
				for(Provider pr: providers){
					providerDTO = new ProviderDTO();
					providerDTO.setId(pr.getId());
					providerDTO.setAddress(pr.getAddress());
					providerDTO.setFullName(pr.getFullName());
					providerDTO.setTelNumber(pr.getTelNumber());
					providerDTO.setEmail(pr.getEmail());
					providerDTOs.add(providerDTO);
				}
				
				Map<String, Object> listAllProvider = new HashMap<String, Object>();
				listAllProvider.put("providerDTOs", providerDTOs);
				return new ModelAndView("viewProvider", "listAllProvider",listAllProvider);
			}
		}
		return new ModelAndView("viewProvider","",null);
	}

	@RequestMapping(value = "/addProvider", method = RequestMethod.GET)
	public ModelAndView addProvider() {
		System.out.println("addprovider");
		return new ModelAndView("addProvider","",null);
	}
	
	@RequestMapping(value = "/addProvider", method = RequestMethod.POST)
	public String addProvider(HttpServletRequest request, ModelMap model,
			@ModelAttribute("ProviderDTO") ProviderDTO providerDTO) {
		if (providerDTO != null) {
			provider = new Provider();
			provider.setAddress(providerDTO.getAddress());
			provider.setEmail(providerDTO.getEmail());
			provider.setFullName(providerDTO.getFullName());
			provider.setTelNumber(providerDTO.getTelNumber());
			this.providerBusiness.addProvider(provider);
		} 
		return "redirect:/viewProvider";
	}

	@RequestMapping(value = "/editProvider", method = RequestMethod.GET)
	public ModelAndView editProvider(HttpServletRequest request, ModelMap model,int id) {
		Provider provider = providerBusiness.getProviderById(id);
		if(provider != null){
			providerDTO = new ProviderDTO();
			providerDTO.setId(provider.getId());
			providerDTO.setFullName(provider.getFullName());
			providerDTO.setAddress(provider.getAddress());
			providerDTO.setTelNumber(provider.getTelNumber());
			providerDTO.setEmail(provider.getEmail());
		   return new ModelAndView("updateProvider", "providerDTO", providerDTO);
		}
		return new ModelAndView("viewProvider","",null);
	}

	@RequestMapping(value = "/editProvider", method = RequestMethod.POST)
	public String editProvider(HttpServletRequest request, ModelMap model,
			@ModelAttribute("ProviderDTO") ProviderDTO providerDTO) {
		if (providerDTO != null) {
			Provider provider = new Provider();
			provider.setId(providerDTO.getId());
			provider.setAddress(providerDTO.getAddress());
			provider.setEmail(providerDTO.getEmail());
			provider.setFullName(providerDTO.getFullName());
			provider.setTelNumber(providerDTO.getTelNumber());
			
			this.providerBusiness.saveProviderChanged(provider);
		}
		return "redirect:/viewProvider";
	}

	@RequestMapping(value = "/deleteProvider", method = RequestMethod.GET)
	public String deleteProvider(HttpServletRequest request, ModelMap model,
			  int id) {
		providerBusiness.deleteProvider(id);
		return "redirect:/viewProvider";
	}
	
	@RequestMapping(value = "/searchProvider", method = RequestMethod.GET)
	public ModelAndView searchProvider(HttpServletRequest request, ModelMap model,
			  String key) {
		byte[] tmp = key.getBytes();
		String value="";
		try {
			value = new String(tmp,"UTF-8");
			request.setCharacterEncoding("UTF-8");
			 
			System.out.println(value);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Provider> providers;
		List<ProviderDTO> providerDTOs = new ArrayList<ProviderDTO>();
		providers = providerBusiness.getListProviderByKey(value);
		
		Map<String, Object> providerModel = new HashMap<String, Object>();
		if(providers!=null){
			for(Provider c: providers){
				ProviderDTO providerDTO = new ProviderDTO();
				providerDTO.setId(c.getId());
				providerDTO.setTelNumber(c.getTelNumber());
				providerDTO.setFullName(c.getFullName());
				providerDTO.setEmail(c.getEmail());
				providerDTO.setAddress(c.getAddress());
				providerDTOs.add(providerDTO);	
			}
			providerModel.put("providerDTOs",providerDTOs);
		}
		return new ModelAndView("providerSearchResult","providerModel",providerModel);
	}

}
